import service from '@/service/service'

import i18n from '@/i18n'
import Vue from 'vue'

let vueI18n = new Vue({
  i18n
})

const freqSegment = {
  state: {
    checkRules: {
      up_beam: [
        // 请选择上行波束
        { required: true, message: vueI18n.$t('message.validate.resources.freqSegment.up_beam'), trigger: 'blur' }
      ],
      up_freq_begin: [
        // 请输入上行起始频点
        { required: true, message: vueI18n.$t('message.validate.resources.freqSegment.up_freq_begin'), trigger: 'blur' }
      ],
      up_freq_end: [
        // 请输入上行终止频点
        { required: true, message: vueI18n.$t('message.validate.resources.freqSegment.up_freq_end'), trigger: 'blur' }
      ],
      down_freq_begin: [
        // 请输入下行起始频点
        { required: true, message: vueI18n.$t('message.validate.resources.freqSegment.down_freq_begin'), trigger: 'blur' },
        { type: 'number', message: vueI18n.$t('message.validate.common.number'), trigger: 'blur' }
      ],
      down_freq_end: [
        // 请输入下行终止频点
        { required: true, message: vueI18n.$t('message.validate.resources.freqSegment.down_freq_end'), trigger: 'blur' },
        { type: 'number', message: vueI18n.$t('message.validate.common.number'), trigger: 'blur' }
      ]
    },
    timePeriodCheckRules: {
      start_time: [
        // 请选择开始时间
        { required: true, message: vueI18n.$t('message.validate.resources.timePeriod.start_time'), trigger: 'blur' }
      ],
      end_time: [
        // 请选择结束时间
        { required: true, message: vueI18n.$t('message.validate.resources.timePeriod.end_time'), trigger: 'blur' }
      ]
    },

    freqSegmentTable: []
  },
  mutations: {
    setFreqSegmentTable (state, data) {
      state.freqSegmentTable = data
    },
    setFreqSegmentEnable (state, payload) {
      state.freqSegmentTable[payload.index].enable = payload.enable
    },
    clearFreqSegmentTable (state) {
      state.freqSegmentTable = []
    }
  },
  actions: {
    async getFreqSegmentTableData (context, payload) {
      let paramKey = ''
      switch (payload.node_type) {
        case 'satellite':
          paramKey = 'transponder__down_beam__satellite'
          break
        case 'beam':
          paramKey = 'transponder__down_beam'
          break
        case 'transponder':
          paramKey = 'transponder'
          break
        default:
          paramKey = 'transponder'
      }
      let response = await context.dispatch(`axios`, {
        method: 'get',
        url: `/api/abs/resources/freq_segment_with_time/`,
        params: {
          [paramKey]: payload.id
        }
      })
      context.commit(`setFreqSegmentTable`, response.data)
    }
  },
  getters: {
    getFreqSegmentTableData: (state) => {
      return [...state.freqSegmentTable.map(item => {
        return {
          ...item,
          down_freq_begin: item.down_freq_begin / 1000,
          down_freq_end: item.down_freq_end / 1000,
          down_freq_begin_format: service.miliFormat(item.down_freq_begin / 1000),
          down_freq_end_format: service.miliFormat(item.down_freq_end / 1000)
        }
      })]
    }
  }
}
export default freqSegment
